//
//  MyOrderOrderDtailViewController.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-21.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyOrderOrderDtailViewController.h"
#import "URLFactory.h"
#import "CONSTS.h"
#import "OrderBaseInfo.h"
#import "SubOrderViewController.h"
#import "WuLiuDetailViewController.h"
#import "ImageButton.h"
#import "GoodDetailWebViewController.h"
#import "XiuGaiBeiZhuViewController.h"
#import "CloseOrderViewController.h"
#import "CustomNavigationController.h"



@interface MyOrderOrderDtailViewController()

- (void)viewGoodDetail:(ImageButton *)sender;
- (void)actionForOrder;

- (void)hiddenCustomPickView:(CustomPickView *)customPickView_;
- (void)customPicViewAnimationIsStop;

- (void)yanChangShouHuoShiJian:(NSString *)yanChangShiJian_;
- (void)yanChangShouHuoShiJianServerIsRetrun:(TaoBaoServer *)yanChangShouHuoShiJianTaoBaoSever_;

@end


@implementation MyOrderOrderDtailViewController

@synthesize tid                                 =           _tid;
@synthesize tableViewDataSource                 =           _tableViewDataSource;
@synthesize getOrderDetailTaoBaoSever           =           _getOrderDetailTaoBaoSever;
@synthesize yanChangShouHuoShiJianTaoBaoSever   =           _yanChangShouHuoShiJianTaoBaoSever;


- (void)dealloc
{
    LF_RELEASE_SAFELY(_tid);
    LF_RELEASE_SAFELY(_tableViewDataSource);
    LF_RELEASE_SAFELY(_getOrderDetailTaoBaoSever);
    LF_RELEASE_SAFELY(_yanChangShouHuoShiJianTaoBaoSever);
    
    [_tableView release];
    [super dealloc];
}

- (id)initWithTid:(NSString *)tid_
{
    self = [super init];
    if (self) 
    {
        self.tid = tid_;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"订单详情";
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"操作" 
                                                                               style: UIBarButtonItemStylePlain 
                                                                              target: self 
                                                                              action: @selector(actionForOrder)] autorelease];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    _tableView.alpha = 0.0f;
    
    NSString *api_taobao_trade_fullinfo_get_str = [URLFactory get_taobao_trade_fullinfo_getWithTid: _tid];
    
    self.getOrderDetailTaoBaoSever = [[[TaoBaoServer alloc] initWithUrl: api_taobao_trade_fullinfo_get_str 
                                                                 object: self 
                                                     requestIsEndSelect: @selector(didGetOrderDetail:) 
                                                    requestIsFailSelect: nil  
                                                           showWaitView: YES 
                                                              urlEncode: NO] autorelease];
    
    [_getOrderDetailTaoBaoSever startAsynchronousRequest];
}

- (void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
    
    disSelctCell(_tableView);
}

- (void)viewDidUnload
{
    [_tableView release];
    _tableView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



#pragma mark - 自定义函数
- (void)didGetOrderDetail:(TaoBaoServer *)getOrderDetailTaoBaoSever_
{
#ifdef  DEBUG
    ITTDINFO(@"%@", getOrderDetailTaoBaoSever_.dic);
    writeJsonToPlist(getOrderDetailTaoBaoSever_.dic, api_taobao_trade_fullinfo_get);
#endif
    
    if (!verifyServerRetunSuccess( getOrderDetailTaoBaoSever_.dic))
    {
        showAlert(ALERT_TITLE, ALERT_MESSAGE);
        
        [_tableView removeFromSuperview];
        
        return;
    }
    
    OrderBaseInfo *orderBaseInfo = [[[OrderBaseInfo alloc] init] autorelease];
    NSDictionary *tradeDic = [[getOrderDetailTaoBaoSever_.dic objectForKey: @"trade_fullinfo_get_response"] objectForKey: @"trade"];
    NSArray *ordersArray = [[tradeDic objectForKey: @"orders"] objectForKey: @"order"];
    NSMutableArray *orders = [NSMutableArray array];
    
    //解析子订单
    for (NSDictionary *orderDic in ordersArray)
    {
        ITTDINFO(@"%@", orderDic);
        Order *order = [[[Order alloc] init] autorelease];
        
        NSString *title = [orderDic objectForKey: @"title"];
        order.title = title;
        ITTDINFO( @"%@", order.title);
        
        NSString *status = [orderDic objectForKey: @"status"];
        order.status = getOrderStatusByStr(status);
        ITTDINFO(@"%@", order.status);
        
        NSString *price = [NSString stringWithFormat: @"%@", [orderDic objectForKey: @"price"]];
        order.price = price;
        ITTDINFO(@"%@", order.price);
        
        NSString *num = [NSString stringWithFormat: @"%@", [orderDic objectForKey: @"num"]];
        order.num = num;
        ITTDINFO(@"%@", order.num);
        
        NSString *total_fee = [NSString stringWithFormat: @"%@", [orderDic objectForKey: @"total_fee"]];
        order.total_fee = total_fee;
        ITTDINFO(@"%@", order.total_fee);
        
        NSString *payment = [NSString stringWithFormat: @"%@", [orderDic objectForKey: @"payment"]];
        order.payment = payment;
        ITTDINFO(@"%@", order.payment);
        
        //因为官方给的图片太大，所以我找到了taoba的商品的图片的规律，只要把.jpg用jpg_sum.jpg替换就可以得到小的图片
        order.pic_path = [[orderDic objectForKey: @"pic_path"] stringByReplacingOccurrencesOfString: @".jpg" withString: @".jpg_sum.jpg"];
        ITTDINFO(@"%@", order.pic_path);
        
        id num_iid = [orderDic objectForKey: @"num_iid"];   //有可能是NSNumber
        order.num_iid =[NSString stringWithFormat: @"%@", num_iid];
        ITTDINFO(@"%@", order.num_iid);
        
        NSString *post_fee = [tradeDic objectForKey: @"post_fee"];
        order.post_fee = post_fee;
        
        ITTDINFO(@"%@", [orderDic objectForKey: @"oid"]);
        order.oid = [NSString stringWithFormat: @"%@", [orderDic objectForKey: @"oid"]];
        
        ITTDINFO(@"%@", order.oid);
        
        [orders addObject: order];
    }
    orderBaseInfo.orders = orders;
    
    
    
    /*
     @synthesize order;
     @synthesize status;
     @synthesize tid;
     @synthesize created;
     @synthesize timeout_action_time;
     @synthesize alipay_no;
     @synthesize price;
     @synthesize num;
     */
    
    NSString *status = [tradeDic objectForKey: @"status"];
    orderBaseInfo.status = getOrderStatusByStr( status );
    ITTDINFO(@"%@", orderBaseInfo.status);
    
    NSString *tid = [NSString stringWithFormat: @"%@", [tradeDic objectForKey: @"tid"]];
    orderBaseInfo.tid = tid;
    ITTDINFO(@"%@", orderBaseInfo.tid);
    
    NSString *created = [tradeDic objectForKey: @"created"];
    orderBaseInfo.created = created;
    ITTDINFO(@"%@", orderBaseInfo.created);
    
    NSString *timeout_action_time = [tradeDic objectForKey: @"timeout_action_time"];
    if (!timeout_action_time || [timeout_action_time isEqualToString: @""])
    {
        timeout_action_time = @"交易已完成或关闭";
    }
    orderBaseInfo.timeout_action_time = timeout_action_time;
    ITTDINFO(@"%@", orderBaseInfo.timeout_action_time);
    
    NSString *alipay_no = [NSString stringWithFormat: @"%@", [tradeDic objectForKey: @"alipay_no"]];
    orderBaseInfo.alipay_no = alipay_no;
    ITTDINFO(@"%@", orderBaseInfo.alipay_no);
    
    NSString *title = [NSString stringWithFormat: @"%@", [[[[tradeDic objectForKey: @"orders"] objectForKey: @"order"] objectAtIndex: 0] objectForKey: @"title"]];
    orderBaseInfo.title = title;
    
    //    NSString *price = [NSString stringWithFormat: @"%@", [tradeDic objectForKey: @"price"]];
    //    orderBaseInfo.price = price;
    //    ITTDINFO(@"%@", orderBaseInfo.price);
    //    
    //    NSString *num = [NSString stringWithFormat: @"%@", [tradeDic objectForKey: @"num"]];
    //    orderBaseInfo.num = num;
    //    ITTDINFO(@"%@", orderBaseInfo.num);
    
    NSString *discount_fee = [NSString stringWithFormat: @"%@", [tradeDic objectForKey: @"discount_fee"]];
    orderBaseInfo.discount_fee = discount_fee;
    
    NSString *post_fee = [NSString stringWithFormat: @"%@", [tradeDic objectForKey: @"post_fee"]];
    orderBaseInfo.post_fee = post_fee;
    
    NSString *payment = [NSString stringWithFormat: @"%@", [tradeDic objectForKey: @"payment"]];
    orderBaseInfo.payment = payment;
    
    NSString *total_fee = [NSString stringWithFormat: @"%@", [tradeDic objectForKey: @"total_fee"]];
    orderBaseInfo.total_fee = total_fee;
    
    NSString *buyer_nick = [tradeDic objectForKey: @"buyer_nick"];
    orderBaseInfo.buyer_nick = buyer_nick;
    
    NSString *buyer_email = [tradeDic objectForKey: @"buyer_email"];
    orderBaseInfo.buyer_email = buyer_email;
    
    NSString *receiver_name = [tradeDic objectForKey: @"receiver_name"];
    orderBaseInfo.receiver_name = receiver_name;
    
    NSString *buyer_area = [tradeDic objectForKey: @"buyer_area"];
    orderBaseInfo.buyer_area = buyer_area;
    
    NSString *receiver_phone = [tradeDic objectForKey: @"receiver_phone"];
    orderBaseInfo.receiver_phone = receiver_phone;
    
    NSString *buyer_alipay_no = [tradeDic objectForKey: @"buyer_alipay_no"];
    orderBaseInfo.buyer_alipay_no = buyer_alipay_no;
    
    
    //解析收货信息
    NSString *receiver_state = [tradeDic objectForKey: @"receiver_state"];
    orderBaseInfo.receiver_state = receiver_state;
    
    NSString *receiver_district = [tradeDic objectForKey: @"receiver_district"];
    orderBaseInfo.receiver_district = receiver_district;
    
    NSString *receiver_city = [tradeDic objectForKey: @"receiver_city"];
    orderBaseInfo.receiver_city = receiver_city;
    
    NSString *receiver_address = [tradeDic objectForKey: @"receiver_address"];
    orderBaseInfo.receiver_address = receiver_address;
    
    NSString *yunSongFangShi = [tradeDic objectForKey: @"shipping_type"];
    // ems = ems, express = 快递, post = 平邮, free = 免费, virtual = 虚拟。
    if ([yunSongFangShi isEqualToString: @"ems"]) 
    {
        yunSongFangShi = @"ems快递";
    }else if ([yunSongFangShi isEqualToString: @"express"])
    {
        yunSongFangShi = @"快递";
    }else if ([yunSongFangShi isEqualToString: @"post"])
    {
        yunSongFangShi = @"平邮";
    }else if ([yunSongFangShi isEqualToString: @"free"])
    {
        yunSongFangShi = @"免费";
    }else
    {
        yunSongFangShi = @"虚拟";
    }
    orderBaseInfo.shipping_type = yunSongFangShi;
    
    
    NSString *seller_memo = [tradeDic objectForKey: @"seller_memo"];
    if (!seller_memo || [seller_memo isEqualToString: @""])
    {
        seller_memo = @"卖家没有填写备忘";
    }
    orderBaseInfo.seller_memo = seller_memo;
    
    NSString *buyer_message = [tradeDic objectForKey: @"buyer_message"];
    if (!buyer_message || [buyer_message isEqualToString: @""])
    {
        buyer_message = @"买家暂时没有留言";
    }
    orderBaseInfo.buyer_message = buyer_message; 
    
    
    self.tableViewDataSource = orderBaseInfo;
    
    [_tableView reloadData];
    
    self.navigationItem.rightBarButtonItem.enabled = YES;
    
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationDuration: 0.3];
    
    _tableView.alpha = 1.0f;
    
    [UIView commitAnimations];
}

- (void)viewGoodDetail:(ImageButton *)sender
{
    NSString *num_iid = sender.num_iid;
    GoodDetailWebViewController *goodDetailWebViewController = [[[GoodDetailWebViewController alloc] initWithNum_iid: num_iid] autorelease];
    
    [self.navigationController pushViewController: goodDetailWebViewController animated: YES];
}

- (void)actionForOrder
{
    /*
     交易状态。可选值: 
     * TRADE_NO_CREATE_PAY(没有创建支付宝交易) 
     
     * WAIT_BUYER_PAY(等待买家付款) 
     
     * WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
     
     * WAIT_BUYER_CONFIRM_GOODS(等待买家确认收货,即:卖家已发货)
     
     * TRADE_BUYER_SIGNED(买家已签收,货到付款专用)
     
     * TRADE_FINISHED(交易成功) 
     
     * TRADE_CLOSED(付款以后用户退款成功，交易自动关闭) 
     
     * TRADE_CLOSED_BY_TAOBAO(付款以前，卖家或买家主动关闭交易)
     
     */
    
    UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: @"处理订单" 
                                                              delegate: self
                                                     cancelButtonTitle: nil
                                                destructiveButtonTitle: nil 
                                                     otherButtonTitles: nil] autorelease];
    
    NSString *status = _tableViewDataSource.status;
    
    
    if ([status isEqualToString: WAIT_BUYER_PAY])                       
    {
        //1）只有交易状态为“等待买家付款”时，卖家才可以操作关闭订单交易。
        //2）如果有两个商品，也可以选择只关闭其中一个商品的交易
        actionSheet = [[[UIActionSheet alloc] initWithTitle: @"处理订单" 
                                                   delegate: self
                                          cancelButtonTitle: nil
                                     destructiveButtonTitle:  GUAN_BI_JIAO_YI 
                                          otherButtonTitles: nil] autorelease];
        
        
    }
    
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    
    if ([status isEqualToString: WAIT_SELLER_SEND_GOODS])
    {
        [actionSheet addButtonWithTitle: FA_HUO];
    }
    
    /*
    if ([status isEqualToString: WAIT_BUYER_PAY]) {
        //买家拍下宝贝未付款时，卖家可以修改交易的金额。
     //此接口尚未开通
        [actionSheet addButtonWithTitle: XIU_GAI_JIA_GE];
        
    }
     */
    
    if ([status isEqualToString: WAIT_BUYER_CONFIRM_GOODS])
    {
        [actionSheet addButtonWithTitle: YAN_CHANG_FA_HUO_SHI_JIAN];
    }
    
    [actionSheet addButtonWithTitle: BEI_WANG];
    
    [actionSheet addButtonWithTitle: @"返回"];
    
    [actionSheet showInView: self.navigationController.view];
}

//外部调用方法，当修改完备忘后，调用此方法以刷新本页面的备忘
- (void)xiuGaiBeiZhu:(NSString *)newBeiZhu_
{
    _tableViewDataSource.seller_memo = newBeiZhu_;
    [_tableView reloadData];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (1 == section)
    {
        return [_tableViewDataSource.orders count];
    }
    
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}

#define JIAN_JU 8
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *orderDetailCellIdentifier = @"orderDetailCell";
    static NSString *baoBeiXiangQiangCellIdentifier = @"baoBeiXiangQiangCell";
    static NSString *maiJiaXinXiCellIdentifier = @"maiJiaXinXiCell";
    static NSString *wuLiuXinXiCellIdentifier = @"wuLiuXinXiCell";
    static NSString *maiJiaLiuYanCellIdentifier = @"maiJiaLiuYanCell";
    static NSString *beiWangXinXiCellIdentifier = @"beiWangXinXiCell";
    
    
    
    UITableViewCell *cell = nil;
    
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    
    if (0 == section)   //订单基本信息
    {
        cell = [tableView dequeueReusableCellWithIdentifier: orderDetailCellIdentifier];
        if (cell == nil)
        {
            NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
            cell = [cellArray objectAtIndex: 0];
        }
        
        /*
         @synthesize order;
         @synthesize status;
         @synthesize tid;
         @synthesize created;
         @synthesize timeout_action_time;
         @synthesize alipay_no;
         @synthesize price;
         @synthesize num;
         */
        
        UILabel *orderStatusLabel = (UILabel *)[cell viewWithTag: 101];
        orderStatusLabel.text = _tableViewDataSource.status;
        CGSize size = bubbleView(orderStatusLabel.text, orderStatusLabel.font, orderStatusLabel.frame.size.width);
        
        float height = size.height - orderStatusLabel.frame.size.height;
        
        orderStatusLabel.frame = CGRectMake(orderStatusLabel.frame.origin.x,
                                            orderStatusLabel.frame.origin.y, 
                                            orderStatusLabel.frame.size.width,
                                            size.height);
        
        UILabel *label = nil;
        UILabel *orderTidLabel = (UILabel *)[cell viewWithTag: 102];
        orderTidLabel.text = _tableViewDataSource.tid;
        setCellLabelFrame(&orderTidLabel, height);
        label = (UILabel *)[cell viewWithTag: 109];
        setCellLabelFrame(&label, height);
        
        UILabel *orderCreatedLabel = (UILabel *)[cell viewWithTag: 103];
        orderCreatedLabel.text = _tableViewDataSource.created;
        setCellLabelFrame(&orderCreatedLabel, height);
        label = (UILabel *)[cell viewWithTag: 110];
        setCellLabelFrame(&label, height);
        
        UILabel *orderTimeout_action_timeLabel = (UILabel *)[cell viewWithTag: 104];
        orderTimeout_action_timeLabel.text = _tableViewDataSource.timeout_action_time;
        setCellLabelFrame(&orderTimeout_action_timeLabel, height);
        label = (UILabel *)[cell viewWithTag: 111];
        setCellLabelFrame(&label, height);
        
        UILabel *orderAlipy_noLabel = (UILabel *)[cell viewWithTag: 105];
        orderAlipy_noLabel.text = _tableViewDataSource.alipay_no;
        setCellLabelFrame(&orderAlipy_noLabel, height);
        label = (UILabel *)[cell viewWithTag: 112];
        setCellLabelFrame(&label, height);
        
        UILabel *orderTotal_feeLabel = (UILabel *)[cell viewWithTag: 106];
        orderTotal_feeLabel.text = _tableViewDataSource.total_fee;
        setCellLabelFrame(&orderTotal_feeLabel, height);
        label = (UILabel *)[cell viewWithTag: 113];
        setCellLabelFrame(&label, height);
        
        UILabel *orderPost_feeLabel = (UILabel *)[cell viewWithTag: 107];
        orderPost_feeLabel.text = _tableViewDataSource.post_fee;
        setCellLabelFrame(&orderPost_feeLabel, height);
        label = (UILabel *)[cell viewWithTag: 114];
        setCellLabelFrame(&label, height);
        
        UILabel *orderPaymentLabel = (UILabel *)[cell viewWithTag: 108];
        orderPaymentLabel.text = _tableViewDataSource.payment;
        setCellLabelFrame(&orderPaymentLabel, height);
        label = (UILabel *)[cell viewWithTag: 115];
        setCellLabelFrame(&label, height);
    }
    
    else if (1 == section)  //宝贝/子订单
    {
        cell = [tableView dequeueReusableCellWithIdentifier: baoBeiXiangQiangCellIdentifier];
        if (cell == nil)
        {
            NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
            cell = [cellArray objectAtIndex: 1];
            
            ImageButton *imageButton = (ImageButton *)[cell viewWithTag: 102];
            imageButton.layer.cornerRadius = 70.0;
            imageButton.layer.shadowColor = [UIColor blackColor].CGColor;
            imageButton.layer.shadowOpacity = 0.3;
            imageButton.layer.shadowOffset = CGSizeMake(4, 5);
        }
        
        Order *order = [_tableViewDataSource.orders objectAtIndex: row];
        UILabel *titlLabel = (UILabel *)[cell viewWithTag: 101];
        titlLabel.text = order.title;
        
        CGSize size = bubbleView( titlLabel.text, titlLabel.font, titlLabel.frame.size.width);
        titlLabel.frame = CGRectMake(titlLabel.frame.origin.x, titlLabel.frame.origin.y, titlLabel.frame.size.width, size.height);
        
        ImageButton *button = (ImageButton *)[cell viewWithTag: 102];
        [button addTarget: self action: @selector(viewGoodDetail:) forControlEvents: UIControlEventTouchUpInside];
        NSURL *url = [NSURL URLWithString: order.pic_path];
        [button setImageWithURL: url placeholderImage: [UIImage imageNamed: @"goodPic.jpeg"]];
        button.num_iid = order.num_iid;
    }
    
    else if (2 == section)
    {
        cell = [tableView dequeueReusableCellWithIdentifier: maiJiaXinXiCellIdentifier];
        if (cell == nil)
        {
            NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
            cell = [cellArray objectAtIndex: 2];
        }
        
        /*
         @synthesize buyer_nick;
         @synthesize buyer_email;
         @synthesize receiver_name;
         @synthesize buyer_area;
         @synthesize receiver_phone;
         @synthesize buyer_alipay_no;
         */
        
        UILabel *buyer_nickLabel = (UILabel *)[cell viewWithTag: 101];
        buyer_nickLabel.text = _tableViewDataSource.buyer_nick;
        
        UILabel *buyer_emailLabel = (UILabel *)[cell viewWithTag: 102];
        buyer_emailLabel.text = _tableViewDataSource.buyer_email;
        
        UILabel *receiver_nameLabel = (UILabel *)[cell viewWithTag: 103];
        receiver_nameLabel.text = _tableViewDataSource.receiver_name;
        
        UILabel *receiver_districtLabel = (UILabel *)[cell viewWithTag: 104];
        receiver_districtLabel.text = _tableViewDataSource.receiver_district;
        
        UILabel *receiver_phoneLabel = (UILabel *)[cell viewWithTag: 105];
        receiver_phoneLabel.text = _tableViewDataSource.receiver_phone;
        
        UILabel *buyer_alipay_noLabel = (UILabel *)[cell viewWithTag: 106];
        buyer_alipay_noLabel.text = _tableViewDataSource.buyer_alipay_no;
    }
    
    else if (3 == section)  //物流信息
    {
        cell = [tableView dequeueReusableCellWithIdentifier: wuLiuXinXiCellIdentifier];
        if (cell == nil)
        {
            NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
            cell = [cellArray objectAtIndex: 3];
        }
        
        UILabel *shouHuoDiZhiLabel = (UILabel *)[cell viewWithTag: 101];
        shouHuoDiZhiLabel.text = [NSString stringWithFormat: @"%@%@%@%@", 
                                  _tableViewDataSource.receiver_state, 
                                  _tableViewDataSource.receiver_district, 
                                  _tableViewDataSource.receiver_city, 
                                  _tableViewDataSource.receiver_address];
        
        CGSize size1 = bubbleView(shouHuoDiZhiLabel.text, shouHuoDiZhiLabel.font, shouHuoDiZhiLabel.frame.size.width);
        shouHuoDiZhiLabel.frame = CGRectMake(shouHuoDiZhiLabel.frame.origin.x, 
                                             shouHuoDiZhiLabel.frame.origin.y, 
                                             shouHuoDiZhiLabel.frame.size.width, 
                                             size1.height);
        
        UILabel *yunSongFangShiLabel = (UILabel *)[cell viewWithTag: 102];
        yunSongFangShiLabel.text = _tableViewDataSource.shipping_type;
        
        CGSize size2 = bubbleView(yunSongFangShiLabel.text, 
                                  yunSongFangShiLabel.font, 
                                  yunSongFangShiLabel.frame.size.width);
        
        yunSongFangShiLabel.frame = CGRectMake(yunSongFangShiLabel.frame.origin.x, 
                                               shouHuoDiZhiLabel.frame.origin.y + shouHuoDiZhiLabel.frame.size.height + JIAN_JU, 
                                               yunSongFangShiLabel.frame.size.width, 
                                               size2.height);
        
        
        UILabel *label = (UILabel *)[cell viewWithTag: 103];
        label.center = CGPointMake(label.center.x, yunSongFangShiLabel.center.y);
        
        
    }
    
    else if (4 == section)  //买家留言
    {
        cell = [tableView dequeueReusableCellWithIdentifier: maiJiaLiuYanCellIdentifier];
        if (cell == nil)
        {
            NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
            cell = [cellArray objectAtIndex: 4];
        }
        
        UILabel *maiJiaLiuYanLabel = (UILabel *)[cell viewWithTag: 101];
        maiJiaLiuYanLabel.text = _tableViewDataSource.buyer_message;
        
        CGSize size = bubbleView(maiJiaLiuYanLabel.text, maiJiaLiuYanLabel.font, maiJiaLiuYanLabel.frame.size.width);
        maiJiaLiuYanLabel.frame = CGRectMake(maiJiaLiuYanLabel.frame.origin.x, 
                                             maiJiaLiuYanLabel.frame.origin.y,
                                             maiJiaLiuYanLabel.frame.size.width, 
                                             size.height);
        
    }
    
    else// (4 == section && 0 == row)   //卖家备忘
    {
        cell = [tableView dequeueReusableCellWithIdentifier: beiWangXinXiCellIdentifier];
        if (cell == nil)
        {
            NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
            cell = [cellArray objectAtIndex: 5];
        }
        
        UILabel *maiJiaBeiZhuLabel = (UILabel *)[cell viewWithTag: 101];
        maiJiaBeiZhuLabel.text = _tableViewDataSource.seller_memo;
        CGSize size = bubbleView( maiJiaBeiZhuLabel.text, maiJiaBeiZhuLabel.font, maiJiaBeiZhuLabel.frame.size.width);
        
        maiJiaBeiZhuLabel.frame = CGRectMake(maiJiaBeiZhuLabel.frame.origin.x, maiJiaBeiZhuLabel.frame.origin.y, maiJiaBeiZhuLabel.frame.size.width, size.height);
    }
    
    return cell;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    
    if (0 == section)
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
        UITableViewCell *cell = [cellArray objectAtIndex: 0];
        
        UILabel *orderStatusLabel = (UILabel *)[cell viewWithTag: 101];
        
        CGSize size2 = bubbleView(_tableViewDataSource.status, orderStatusLabel.font, orderStatusLabel.frame.size.width);
        
        float height = 238 + (size2.height - orderStatusLabel.frame.size.height);
        
        return height;
    }
    
    else if (1 == section)
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
        UITableViewCell *cell = [cellArray objectAtIndex: 1];
        UILabel *titleLabel = (UILabel *)[cell viewWithTag: 101];
        
        Order *order = [_tableViewDataSource.orders objectAtIndex: row];
        NSString *title = order.title;
        CGSize size = bubbleView( title, titleLabel.font, titleLabel.frame.size.width);
        
        ImageButton *button = (ImageButton *)[cell viewWithTag: 102];
        
        float returnHeight = 0;
        if (button.frame.size.height > size.height)
        {
            returnHeight = button.frame.size.height;
        }else
        {
            returnHeight = size.height;
        }
        
        return returnHeight + titleLabel.frame.origin.y * 2;
    }
    
    else if (2 == section)
    {
        return 179;
    }
    
    else if (3 == section)  //物流信息
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
        UITableViewCell *cell = [cellArray objectAtIndex: 3];
        UILabel *shouHuoDiZhiLabel = (UILabel *)[cell viewWithTag: 101];
        
        NSString *shouHuoDiZhi = [NSString stringWithFormat: @"%@%@%@%@", 
                                  _tableViewDataSource.receiver_state, 
                                  _tableViewDataSource.receiver_district, 
                                  _tableViewDataSource.receiver_city, 
                                  _tableViewDataSource.receiver_address];
        
        CGSize size1 = bubbleView(shouHuoDiZhi, shouHuoDiZhiLabel.font, shouHuoDiZhiLabel.frame.size.width);
        
        NSString *yunSongFang = _tableViewDataSource.shipping_type;
        UILabel *yunSongFangShiLabel = (UILabel *)[cell viewWithTag: 102];
        CGSize size2 = bubbleView(yunSongFang, yunSongFangShiLabel.font, yunSongFangShiLabel.frame.size.width);
        
        float height = shouHuoDiZhiLabel.frame.origin.y + size1.height + JIAN_JU + size2.height + shouHuoDiZhiLabel.frame.origin.y;
        
        return height;;
    }
    
    else if (4 == section)  //买家留言
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
        UITableViewCell *cell = [cellArray objectAtIndex: 4];
        UILabel *maiJiaLiuYanLabel = (UILabel *)[cell viewWithTag: 101];
        ITTDINFO(@"%@", maiJiaLiuYanLabel.text);
        
        
        NSString *maiJiaLiuYan = _tableViewDataSource.buyer_message;
        CGSize size = bubbleView(maiJiaLiuYan, maiJiaLiuYanLabel.font, maiJiaLiuYanLabel.frame.size.width);
        ITTDINFO(@"%@", maiJiaLiuYan);
        
        
        ITTDINFO(@"%@", maiJiaLiuYan);
        
        return size.height + maiJiaLiuYanLabel.frame.origin.y * 2;
    }
    
    else// (4 == section && 0 == row)
    {
        NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed: @"MyOrderOrderDetailCell" owner: nil options: nil];
        UITableViewCell *cell = [cellArray lastObject];
        UILabel *label = (UILabel *)[cell viewWithTag: 101];
        
        CGSize size = bubbleView(_tableViewDataSource.seller_memo, label.font, label.frame.size.width);
        
        return size.height + label.frame.origin.y * 2;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize: 14];
    button.titleLabel.textAlignment = UITextAlignmentLeft;
    [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
    
    UIImage *image = nil;
    if (0 == section)
    {
        image = [UIImage imageNamed: @"cell_section_header_view_top.png"];
        
    }else
    {
        image = [UIImage imageNamed: @"cell_section_header_view_middle.png"];
    }
    [button setBackgroundImage: image forState: UIControlStateNormal];
    
    
    NSString *titleOfButton = nil;
    if (0 == section)
    {
        titleOfButton = @"订单详情";
        [button setTitle: titleOfButton forState: UIControlStateNormal];
        
        return button;
    }
    
    if (1 == section)
    {
        titleOfButton = @"包含子订单";
    }
    
    else if (2 == section)
    {
        titleOfButton = @"买家信息";
    }
    
    else if (3 == section)
    {
        titleOfButton = @"物流信息";
    }
    
    else if (4 == section)
    {
        titleOfButton = @"买家留言";
    }
    
    else// (4 == section && 0 == row)
    {
        titleOfButton = @"备忘信息";
    }
    
    [button setTitle: titleOfButton forState: UIControlStateNormal];
    
    return button;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    if (1 == section)
    {
        Order *order = [_tableViewDataSource.orders objectAtIndex: row];
        SubOrderViewController *subOrderViewController = [[SubOrderViewController alloc] initWithOrder: order];
        ITTDINFO(@"%@", order.num_iid);
        [self.navigationController pushViewController: subOrderViewController animated: YES];
        
        [subOrderViewController release];
    }else if (3 == section)
    {
        WuLiuDetailViewController *wuLiuDetailViewController = [[[WuLiuDetailViewController alloc] initWithTid: _tableViewDataSource.tid] autorelease];
        
        [self.navigationController pushViewController: wuLiuDetailViewController animated: YES];
    }
}

#pragma mark - UIActionSheetDelegate
#define kCUSTOM_PICK_VIEW_TAG 301
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [actionSheet buttonTitleAtIndex: buttonIndex];
    if ([title isEqualToString: BEI_WANG])  //备忘
    {
        NSString *tid = _tableViewDataSource.tid;
        NSString *oldBeiWang = _tableViewDataSource.seller_memo;
        XiuGaiBeiZhuViewController *xiuGaiBeiZhuViewController = [[[XiuGaiBeiZhuViewController alloc] initWithTid: tid 
                                                                                                        oldBeiZhu: oldBeiWang] autorelease];
        xiuGaiBeiZhuViewController.showBeiObject = self;//当修改完备忘后，被页面的备忘也要修改，
        [self.navigationController pushViewController: xiuGaiBeiZhuViewController animated: YES];
    }else if ([title isEqualToString: YAN_CHANG_FA_HUO_SHI_JIAN])   //延长收货时间
    {
        NSArray *customPickViewDataSource = [NSArray arrayWithObjects: @"3天", @"5天", @"7天", @"10天", nil];
        CustomPickView *customPickView = [CustomPickView viewFromNibWithDelegate: self 
                                                                 pickSourceArray: customPickViewDataSource];
        customPickView.tag = kCUSTOM_PICK_VIEW_TAG;
        
        customPickView.frame = CGRectMake(0, self.view.frame.size.height, customPickView.frame.size.width, customPickView.frame.size.height);
        
        [self.view addSubview: customPickView];
        
        self.navigationItem.rightBarButtonItem.enabled = NO;
        
        [UIView beginAnimations: nil context:nil];
        [UIView setAnimationDuration: 0.3];
        
        customPickView.frame = CGRectMake(0, self.view.frame.size.height - customPickView.frame.size.height, customPickView.frame.size.width, customPickView.frame.size.height);
        
        [UIView commitAnimations];
    }else if ([title isEqualToString: GUAN_BI_JIAO_YI]) //关闭交易
    {
        CloseOrderViewController *closeOrderViewController = [[[CloseOrderViewController alloc] initWithOrderBaseInfo: _tableViewDataSource] autorelease];
        
        CustomNavigationController *customNavigationController = [[[CustomNavigationController alloc] initWithRootViewController: closeOrderViewController] autorelease];
        
        [self.navigationController presentModalViewController: customNavigationController  animated: YES];
    }
}

#pragma mark - CustomPickViewDelegate 以及相关的函数

- (void)fixedButtonIsPressed:(CustomPickView *)customPickView
{
    [self hiddenCustomPickView: customPickView];
    
    NSString *yanChangShiJian = [[customPickView getPickValue] stringByReplacingOccurrencesOfString: @"天" withString: @""];
    [self yanChangShouHuoShiJian: yanChangShiJian];
}

- (void)cancelButtonIsPressed:(CustomPickView *)customPickView
{
    [self hiddenCustomPickView: customPickView];

}

- (void)hiddenCustomPickView:(CustomPickView *)customPickView_
{
    [UIView beginAnimations: nil context:nil];
    [UIView setAnimationDuration: 0.3];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDidStopSelector: @selector(customPicViewAnimationIsStop)];
    
    customPickView_.frame = CGRectMake(0, 
                                       self.view.frame.size.height, 
                                       customPickView_.frame.size.width, 
                                       customPickView_.frame.size.height);
    
    [UIView commitAnimations];
    
    self.navigationItem.rightBarButtonItem.enabled = YES;
}

- (void)customPicViewAnimationIsStop
{
    UIView *custmPickView = [self.view viewWithTag: kCUSTOM_PICK_VIEW_TAG];
    
    [custmPickView removeFromSuperview];
}

#pragma mark - 延长收货时间
- (void)yanChangShouHuoShiJian:(NSString *)yanChangShiJian_
{
    NSString *yanQiUrl = [URLFactory get_taobao_trade_receivetime_delay:                                                                    yanChangShiJian_
                                                                    tid: _tableViewDataSource.tid];
    
    self.yanChangShouHuoShiJianTaoBaoSever = [[[TaoBaoServer alloc] initWithUrl: yanQiUrl 
                                                                        object: self 
                                                             requestIsEndSelect: @selector(yanChangShouHuoShiJianServerIsRetrun:)
                                                           requestIsFailSelect: nil 
                                                                  showWaitView: YES
                                                                     urlEncode: NO] autorelease];
    [_yanChangShouHuoShiJianTaoBaoSever startAsynchronousRequest];
}

- (void)yanChangShouHuoShiJianServerIsRetrun:(TaoBaoServer *)yanChangShouHuoShiJianTaoBaoSever_
{
    /*
     {
     "error_response" =     {
     code = 15;
     msg = "Remote service error";
     "sub_code" = "isv.invalid-parameter:tid";
     "sub_msg" = "\U6240\U6709\U5b50\U8ba2\U5355\U90fd\U4e3a[\U9000\U6b3e\U4e2d]\U7684\U8ba2\U5355\U4e0d\U80fd\U5ef6\U957f\U6536\U8d27\U65f6\U95f4";
     };
     }
     */
    BOOL scuessSign = verifyServerRetunSuccess(yanChangShouHuoShiJianTaoBaoSever_.dic);
    if (scuessSign) 
    {
        showAlert(ALERT_TITLE,  @"成功修改收货时间");
    }else
    {
        NSString *reason = [[yanChangShouHuoShiJianTaoBaoSever_.dic objectForKey: @"error_response"] objectForKey: @"sub_msg"];
        if (reason && !([reason isEqualToString: @""]))
        {
            showAlert(ALERT_TITLE, reason);
        }else
        {
        showAlert(ALERT_TITLE, ALERT_MESSAGE);
        }
    }
}

@end
